<template>
  <a-modal
    title="菜单管理"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="客户(商圈)"
        >
          <a-select :disabled="isDisabled"
                    showSearch
                    optionFilterProp="children"
                    :filterOption="filterOption"
                    v-decorator="[ 'code', {rules: [{ required: true, message: '请选择。。。' }]} ]">
            <a-select-option v-for="item in customerList" :key="item.code">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="登录账户"
        >
          <a-input placeholder="登录账户" v-decorator="[ 'username', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="密码"
        >
          <a-input placeholder="密码" type="password"
                   v-decorator="[ 'password', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group buttonStyle="solid"
                         v-decorator="[ 'status', {rules: [{ required: true, message: '请选择' }], initialValue: false}]">
            <a-radio-button :value="false">启用</a-radio-button>
            <a-radio-button :value="true">禁用</a-radio-button>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户昵称"
        >
          <a-input placeholder="用户昵称" v-decorator="['realName', {rules: [{ required: true, message: '请选择' }] }]"/>
        </a-form-item>

        <a-divider/>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="拥有角色"
        >
          <a-select mode="multiple"
                    v-decorator="[ 'roleIds', {rules: [{ required: true, message: '请选择' }]} ]">
            <a-select-option v-for="item in roleList" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate } from '@/api/system/user'
  import { selectAllCustomer } from '@/api/mqpay/customer'
  import { selectAllRole, selectRolesByUserId } from '@/api/system/role'

  export default {
    data () {
      return {
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        customerList: [],
        roleList: [],

        form: this.$form.createForm(this)
      }
    },
    mounted () {
      selectAllCustomer().then(res => {
        this.customerList = res.result
      })
      selectAllRole(3).then(res => {
        this.roleList = res.result
      })
    },
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.id = record.id
        this.visible = true
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        selectRolesByUserId(this.id).then(res => {
          this.$nextTick(() => {
            const formData = pick(record, ['code', 'username', 'password', 'status', 'roleIds', 'realName'])
            formData.roleIds = res.result
            setFieldsValue(formData)
          })
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.type = 3 //客户（商圈）
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
        this.id = null
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      }
    }
  }
</script>
